from django.db import transaction
from django.db.models import Q
from rest_framework import filters, status
from rest_framework.response import Response

from apps.common.models import Pagination
from apps.common.permissions import GzdePermission
from apps.gzde.filters import *
from apps.gzde.models import *
from apps.gzde.serializers import *
from extensions.viewsets import *
from extensions.permissions import IsAuthenticated

# Create your views here.

class GoodsViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """商品管理"""
    serializer_class = GoodsSerializer
    pagination_class = Pagination
    # permission_classes = [IsAuthenticated, GzdePermission]
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter)
    filter_class = GoodsFilter
    search_fields = ['name','detail','category__name']
    select_related_fields = ['video','video_cover','creator','category','updater']
    prefetch_related_fields = ['images','services']
    ordering_fields = ['sort','create_time']

    def get_queryset(self):
        base_query = Q(del_flag=False)
        goods = Goods.objects.filter(base_query)

        return goods
    

class GoodsCategoryViewSet(ModelViewSet, ExportMixin, ImportMixin):
    """商品分类管理"""
    serializer_class = GoodsCategorySerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = GoodsCategoryFilter
    search_fields = ['name']
    select_related_fields = ['parent', 'creator','image','updater']
    ordering_fields = ['sort','create_time']

    def get_queryset(self):
        base_query = Q(del_flag=False) & Q(parent__isnull=True)
        goods_category = GoodsCategory.objects.filter(base_query)

        return goods_category


class AttachmentViewSet(ModelViewSet):
    """Attachment"""

    serializer_class = AttachmentSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = AttachmentFilter
    search_fields = ['name']
    select_related_fields = ['creator', 'updater']
    ordering_fields = ['id', 'name', 'create_time']
    queryset = Attachment.objects.all()

    @transaction.atomic
    def perform_destroy(self, instance):
        instance.file.delete(save=False)
        instance.delete()
        Response(status=status.HTTP_204_NO_CONTENT)


class IndexBannerViewSet(ModelViewSet):
    """IndexBanner"""

    serializer_class = IndexBannerSerializer
    pagination_class = Pagination
    permission_classes = [IsAuthenticated]
    filter_backends = (DjangoFilterBackend, filters.SearchFilter)
    filter_class = IndexBannerFilter
    search_fields = ['name']
    select_related_fields = ['creator', 'updater']
    # ordering_fields = ['sort', 'name', 'create_time']

    def get_queryset(self):
        return IndexBanner.objects.order_by('sort')

    @transaction.atomic
    def perform_destroy(self, instance):
        instance.file.delete(save=False)
        instance.delete()
        Response(status=status.HTTP_204_NO_CONTENT)
